﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Link factory class
/// </summary>
public class Link
{
	// Builds an absolute URL
	private static string BuildAbsolute(string relativeUri)
	{
		// get current uri
		Uri uri = HttpContext.Current.Request.Url;
		// build absolute path
		string app = HttpContext.Current.Request.ApplicationPath;
		if (!app.EndsWith("/")) app += "/";
		relativeUri = relativeUri.TrimStart('/');
		// return the absolute path
		return HttpUtility.UrlPathEncode(String.Format("http://{0}:{1}{2}{3}", uri.Host, uri.Port, app, relativeUri));
	}

	// Generate a page URL
	public static string ToPage(string page, string url)
	{
		return BuildAbsolute(String.Format(url + "?Page={0}", page));
	}

	// Generate a product URL
	public static string ToProduct(string productID)
	{
		return BuildAbsolute(String.Format("Site/ProductDetail.aspx?ProductID={0}", productID));
	}

	// Generate a shopping cart URL
	public static string ToShoppingCart(string productID)
	{
		return BuildAbsolute(String.Format("Site/ShoppingCart.aspx?ProductID={0}", productID));
	}

	// Generate a category URL
	public static string ToCategory(string categoryID)
	{
		return BuildAbsolute(String.Format("Default.aspx?CategoryID={0}", categoryID));
	}
}